#
#将麦系统中商品的生产标准更新为派系统商品的备注
#
#需要在.bahs_profile设置环境变量
#export PSI_PASSWORD=xxxxxxx
#export RDS_AGENT=yyyy
#
#手工在命令行运行 ruby update*.rb
#
require 'mysql2'

rds = Mysql2::Client.new(:host => ENV['RDS_AGENT'], :username => "psi_root", :port => '1401', :password => ENV['PSI_PASSWORD'])

sql1 = 'select * from psi.t_goods'
res1 = rds.query(sql1)

res1.each do |tgr|
    code = tgr['code']
    id = tgr['id']
    name = tgr['name']
    if code
        sql2 = "select standard from ogoods.m_exported where code = '#{code}' limit 0, 1"
        res2 = rds.query(sql2)
        standard = '-'
        if res2.first
            standard = res2.first['standard'] ? res2.first['standard'] : '-'
        end
        memo = standard.gsub(/[,，  ]/,'|').squeeze("|") #将各种分隔符统一替换为竖线，重复的竖线合并
        sql3 = "update psi.t_goods set memo='#{memo}' where id = '#{id}'"
        puts "running: " + sql3
	    #去掉注释以实际执行
        #res3 = rds.query(sql3)
    end
end

rds.close
